package com.cn.demo4.service.impl;

import com.cn.demo4.common.Tool;
import com.cn.demo4.entity.HrEntity;
import com.cn.demo4.mapper.HrMapper;
import com.cn.demo4.service.IHrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class HrServiceImpl implements IHrService {
    @Autowired
    private HrMapper hrMapper;

    @Override
    public List<HrEntity> findAll(HrEntity hrEntity) {
        return hrMapper.findAll(hrEntity);
    }

    @Override
    public int getCount(HrEntity hrEntity) {
        return hrMapper.getCount(hrEntity);
    }

    @Override
    public HrEntity findById(Integer id) {
        return hrMapper.findById(id);
    }

    @Override
    public String add(HrEntity hrEntity) {
        int result = hrMapper.add(hrEntity);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.flushDB();
        }
        return result == 1 ? "success" : "error";
    }

    @Override
    public String update(HrEntity hrEntity) {
        int result = hrMapper.update(hrEntity);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.flushDB();
        }
        return result == 1 ? "success" : "error";
    }

    @Override
    public int del(Integer id) {
        int result = hrMapper.del(id);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.flushDB();
        }
        return result;
    }

    @Override
    public List<HrEntity> getHrModelListForHome() {
        List<HrEntity> list = new ArrayList<>();
        Jedis jedis = new Jedis();
        Boolean exists = jedis.exists("hrId");
        if (exists) {
            //如果id存在则拿到数据库中的数据
            List<String> hrIdList = jedis.lrange("hrId", 0, -1);
            for (int i = 0; i < hrIdList.size(); i++) {
                HrEntity hrEntity = new HrEntity();
                hrEntity.setId(Tool.stringToInt(jedis.lindex("hrId", i)));
                hrEntity.setStatus(Tool.stringToInt(jedis.lindex("hrStatus", i)));
                hrEntity.setHref(jedis.lindex("hrHref", i));
                hrEntity.setTitle(jedis.lindex("hrTitle", i));
                hrEntity.setContentOne(jedis.lindex("hrContentOne", i));
                hrEntity.setContentTwo(jedis.lindex("hrContentTwo", i));
                hrEntity.setImg(jedis.lindex("hrImg", i));
                list.add(hrEntity);
            }
        } else {
            //将从数据库中拿到的数据存入缓存数据库中即redis中
            list = hrMapper.getHrModelListForHome();
            for (int i = 0; i < list.size(); i++) {
                jedis.rpush("hrId", Integer.toString(list.get(i).getId()));
                jedis.rpush("hrStatus", Integer.toString(list.get(i).getStatus()));
                jedis.rpush("hrHref", list.get(i).getHref());
                jedis.rpush("hrTitle", list.get(i).getTitle());
                jedis.rpush("hrContentOne", list.get(i).getContentOne());
                jedis.rpush("hrContentTwo", list.get(i).getContentTwo());
                jedis.rpush("hrImg", list.get(i).getImg());
            }
        }
        return list;
    }
}
